//
//  TwoViewController.m
//  lessonFour
//
//  Created by hh on 16/5/24.
//  Copyright © 2016年 hh. All rights reserved.
//

#import "TwoViewController.h"

@interface TwoViewController ()

@end

@implementation TwoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"第二个ViewController";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"按钮" forState:UIControlStateNormal];
    [btn setTitle:@"按下去了" forState:UIControlStateHighlighted];
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"5"] forState:UIControlStateNormal];
//    [btn sizeToFit];
    btn.layer.borderWidth = 1;
    btn.layer.cornerRadius = 6;
    btn.layer.borderColor = [UIColor blueColor].CGColor;
    btn.frame = CGRectMake(0, 0, 100, 44);
    btn.center = CGPointMake(self.view.frame.size.width/2, 100);
    [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
    [btn addTarget:self action:@selector(onClick2:) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:btn];
    
    UILabel *l = [[UILabel alloc] init];
    l.text = @"UILabel测试";
    [l sizeToFit];
    l.center = CGPointMake(16 + l.frame.size.width/2, 240);
    l.textAlignment = NSTextAlignmentCenter;
    l.tag = 100;
    [self.view addSubview:l];
    
    UISwitch *swi = [[UISwitch alloc] init];
    [swi sizeToFit];
    swi.center = CGPointMake(150, 150);
    [self.view addSubview:swi];
    
    [swi addTarget:self action:@selector(onSwitch:) forControlEvents:UIControlEventValueChanged];
//    UISlider
//    UIProgressView
//    UIActivityIndicatorView
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"左对齐", @"居中", @"右对齐"]];
    [seg sizeToFit];
    seg.center = CGPointMake(160, 190);
    [seg addTarget:self action:@selector(onSeg:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:seg];
}

- (void)onSeg:(UISegmentedControl *)seg
{
    UILabel *l = (UILabel *)[self.view viewWithTag:100];
    [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
        
        if (seg.selectedSegmentIndex == 0) {
            l.center = CGPointMake(16 + l.frame.size.width/2, l.center.y);
        }else if(seg.selectedSegmentIndex == 1){
            l.center = CGPointMake(self.view.frame.size.width/2, l.center.y);
        }else{
            l.center = CGPointMake(self.view.frame.size.width - (16 + l.frame.size.width/2), l.center.y);
        }
    } completion:nil];
}

- (void)onSwitch:(UISwitch *)swi
{
    UILabel *l = (UILabel *)[self.view viewWithTag:100];
    if (swi.isOn) {
        l.layer.borderWidth = 1;
    }else{
        l.layer.borderWidth = 0;
    }
}

- (void)onClick:(UIButton *)sender
{
    sender.backgroundColor = [UIColor whiteColor];
}

- (void)onClick2:(UIButton *)sender
{
    sender.backgroundColor = [UIColor greenColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
